{extend name="public/base"/}

{block name="title"}添加管理员{/block}
{block name="main"}

<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-body">
                <form action="#" method="post" class="row" id="add-form" name="add-form">
                    <div class="form-group col-md-12">
                        <label for="role">所属角色</label>
                        <div class="form-controls">
                            <select class="form-control" id="role" name="role">
                                {volist name="list_role" id="vo"}
                                <option value="{$vo.id}">{$vo.name}</option>
                                {/volist}
                            </select>
                        </div>
                    </div>
                    <div class="form-group col-md-12">
                        <label for="username">用户名</label>
                        <input type="text" class="form-control" id="username" name="username" placeholder="请输入用户名" data-validate="required:请输入用户名"/>
                    </div>
                    <div class="form-group col-md-12">
                        <label for="realname">真实姓名</label>
                        <input type="text" class="form-control" id="realname" name="realname" placeholder="请输入真实姓名" data-validate="required:请输入真实姓名"/>
                    </div>
                    <div class="form-group col-md-12">
                        <label for="password">密码</label>
                        <input type="password" class="form-control" id="password" name="password" placeholder="密码" data-validate="required:请输入密码"/>
                    </div>
                    <div class="form-group col-md-12">
                        <label for="repassword">确认密码</label>
                        <input type="password" class="form-control" id="repassword" name="repassword" value="" placeholder="确认密码" data-validate="required:请输入密码"/>
                    </div>
                    <div class="form-group col-md-12">
                        <label for="status">状态</label>
                        <div class="clearfix">
                            <label class="lyear-radio radio-inline radio-primary">
                                <input type="radio" class="status" id="status" name="status" value="0"><span>禁用</span>
                            </label>
                            <label class="lyear-radio radio-inline radio-primary">
                                <input type="radio" class="status" name="status" value="1" checked><span>启用</span>
                            </label>
                        </div>
                    </div>
                    <div class="form-group col-md-12">
                        <button type="button" id="btn" class="btn btn-primary ajax-post" target-form="add-form">确 定</button>
                        <button type="button" class="btn btn-default" onclick="location.href='/admin/user/index';return false;">返 回</button>
                    </div>
                    <!--</form>-->
                </form>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="js"}
<script type="text/javascript">
    $(function () {
        $('#btn').click(function () {
            let role = $('#role').val();
            let username = $('#username').val();
            let realname = $('#realname').val();
            let password = $('#password').val();
            let repassword = $('#repassword').val();
            let status = $('input:radio[name="status"]:checked').val();
            if (!username) {
                swal('请输入用户名', '', 'warning');
                return false;
            }
            if (password !== repassword) {
                swal('两次输入的密码不一致', '', 'warning');
                return false;
            }
            $.ajax({
                url: "/admin/user/add",
                data: {username: username,realname:realname, password: password, role: role, status: status},
                type: "POST",
                dataType: "JSON",
                success: function (data) {
                    if (data.code === 1) {
                        swal(data.msg, '', 'success')
                            .then(() => {
                                location.reload();
                            });
                    } else {
                        swal(data.msg, '', 'error');
                    }
                }
            });

        });
    });
</script>
{/block}